package com.lsismo.modules.permission.action;


import java.util.List;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.lsismo.core.base.action.BaseAction;
import com.lsismo.core.base.service.GenericManager;
import com.lsismo.modules.permission.model.User;

public class UserAction extends BaseAction {

	
	private static final long serialVersionUID = 1794483079199479510L;
	
	private GenericManager<User, String>  userManager;

	public void setUserManager(GenericManager<User, String> userManager) {
		this.userManager = userManager;
	}
	
	
	 @Action(value = "/userAction", results = { @Result(name = SUCCESS, location = "/manager/modules/user/index.jsp") })
	 public String execute() {
			List<User> userList;
			try {
				userList = userManager.getAll();
				getRequest().setAttribute("DEMO_LIST", userList);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
	 }
	 
	 
	 	@Action(value = "/userAddAction", results = { @Result(name = SUCCESS, location = "/userAction", type = "redirect") })
	    public String add() {
		User user = new User();
		user.setUsername(getRequest().getParameter("username"));
		user.setPassword(getRequest().getParameter("password"));
		try {
			userManager.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	    }

	    @Action(value = "/userDeleteAction", results = { @Result(name = SUCCESS, location = "/userAction", type = "redirect") })
	    public String delete() {
	    	userManager.remove(getRequest().getParameter("id"));
		return SUCCESS;
	    }
	
	
	

}
